package display;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;
import javax.swing.JLayeredPane;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;

import main.main;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;

import calcul.MonteCarlo;


public class myFrame extends JFrame {

	private JPanel contentPane;
	private JTextField stockPrice;
	private JTextField strikePrice;
	private JTextField discountRate;
	private JTextField maturityTime;
	private JTextField carryRate;
	private JTextField volatility;
	private JTextField nStep;
	private JTextField nSim;
	private JLabel resultPrice;
	private JLabel resultTime;
	private JLabel infoPrice;
	private JLabel infoTime;
	private JRadioButtonMenuItem radioN;
	private JRadioButtonMenuItem radioT;
	private JRadioButtonMenuItem set1;
	private JRadioButtonMenuItem set2;
	private JRadioButtonMenuItem set3;
	private JRadioButtonMenuItem set4;
	private JRadioButtonMenuItem set5;
	private JLabel lblNumberOfThread;
	private JTextField threadNB;
	private List<Double> list;

	/**
	 * Create the frame.
	 */
	public myFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 698, 294);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnMode = new JMenu("Mode");
		menuBar.add(mnMode);
		
		radioT = new JRadioButtonMenuItem("Threading");
		mnMode.add(radioT);
			
		radioN = new JRadioButtonMenuItem("Normal");
		radioN.setSelected(true);
		
		ButtonGroup group = new ButtonGroup();
	    group.add(radioT);
	    group.add(radioN);
		
		mnMode.add(radioN);
		
		JMenu mnSelect = new JMenu("Set");
		menuBar.add(mnSelect);
		
		set1 = new JRadioButtonMenuItem("Set1");
		set2 = new JRadioButtonMenuItem("Set2");
		set3 = new JRadioButtonMenuItem("Set3");
		set4 = new JRadioButtonMenuItem("Set4");
		set5 = new JRadioButtonMenuItem("Set5");
		
		set1.setSelected(true);
		
		mnSelect.add(set1);
		mnSelect.add(set2);
		mnSelect.add(set3);
		mnSelect.add(set4);
		mnSelect.add(set5);
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
				
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.setBounds(10, 11, 226, 217);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblStockPrice = new JLabel("Stock price today");
		lblStockPrice.setBounds(10, 14, 109, 14);
		panel.add(lblStockPrice);
		
		stockPrice = new JTextField();
		stockPrice.setBounds(129, 11, 86, 20);
		panel.add(stockPrice);
		stockPrice.setHorizontalAlignment(SwingConstants.CENTER);
		stockPrice.setText("40");
		stockPrice.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Strike price");
		lblNewLabel.setBounds(10, 39, 109, 14);
		panel.add(lblNewLabel);
		
		strikePrice = new JTextField();
		strikePrice.setBounds(129, 36, 86, 20);
		panel.add(strikePrice);
		strikePrice.setHorizontalAlignment(SwingConstants.CENTER);
		strikePrice.setText("50");
		strikePrice.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Discount rate (%)");
		lblNewLabel_1.setBounds(10, 64, 109, 14);
		panel.add(lblNewLabel_1);
		
		discountRate = new JTextField();
		discountRate.setBounds(129, 61, 86, 20);
		panel.add(discountRate);
		discountRate.setHorizontalAlignment(SwingConstants.CENTER);
		discountRate.setText("6");
		discountRate.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Time to maturity");
		lblNewLabel_2.setBounds(10, 89, 109, 14);
		panel.add(lblNewLabel_2);
		
		maturityTime = new JTextField();
		maturityTime.setBounds(129, 86, 86, 20);
		panel.add(maturityTime);
		maturityTime.setHorizontalAlignment(SwingConstants.CENTER);
		maturityTime.setText("0.5");
		maturityTime.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Cost of carry rate (%)");
		lblNewLabel_3.setBounds(10, 114, 109, 14);
		panel.add(lblNewLabel_3);
		
		carryRate = new JTextField();
		carryRate.setBounds(129, 111, 86, 20);
		panel.add(carryRate);
		carryRate.setHorizontalAlignment(SwingConstants.CENTER);
		carryRate.setText("10");
		carryRate.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Volatility (%)");
		lblNewLabel_4.setBounds(10, 139, 109, 14);
		panel.add(lblNewLabel_4);
		
		volatility = new JTextField();
		volatility.setBounds(129, 136, 86, 20);
		panel.add(volatility);
		volatility.setHorizontalAlignment(SwingConstants.CENTER);
		volatility.setText("45");
		volatility.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("Number of step");
		lblNewLabel_5.setBounds(10, 164, 109, 14);
		panel.add(lblNewLabel_5);
		
		nStep = new JTextField();
		nStep.setBounds(129, 161, 86, 20);
		panel.add(nStep);
		nStep.setHorizontalAlignment(SwingConstants.CENTER);
		nStep.setText("168");
		nStep.setColumns(10);
		
		JLabel lblNumberOfSimulation = new JLabel("Number of simulation");
		lblNumberOfSimulation.setBounds(10, 189, 109, 14);
		panel.add(lblNumberOfSimulation);
		
		nSim = new JTextField();
		nSim.setBounds(129, 186, 86, 20);
		panel.add(nSim);
		nSim.setHorizontalAlignment(SwingConstants.CENTER);
		nSim.setText("100000");
		nSim.setColumns(10);
		
		JButton btnNewButton = new JButton("Run Simulation");
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				HashMap<String, Object> result = main.calcul(
							MonteCarlo.PUT, 	
							radioN.isSelected() ? main.NORMAL : Integer.valueOf(threadNB.getText()),
							Double.valueOf(stockPrice.getText()), 
							Double.valueOf(strikePrice.getText()), 
							Double.valueOf(maturityTime.getText()), 
							Double.valueOf(discountRate.getText()), 
							Double.valueOf(carryRate.getText()), 
							Double.valueOf(volatility.getText()), 
							Double.valueOf(nStep.getText()), 
							Double.valueOf(nSim.getText()));
				
				resultPrice.setText(result.get("value").toString());
				resultTime.setText(result.get("time").toString());
			}
		});
		
		btnNewButton.setBounds(246, 11, 174, 53);
		contentPane.add(btnNewButton);
		
		infoPrice = new JLabel("Value of the Option : ");
		infoPrice.setBounds(460, 20, 136, 14);
		contentPane.add(infoPrice);
		
		resultPrice = new JLabel("Waiting for data...");
		resultPrice.setBounds(500, 40, 136, 14);
		contentPane.add(resultPrice);
		
		infoTime = new JLabel("Calculation time : ");
		infoTime.setBounds(460, 80, 136, 14);
		contentPane.add(infoTime);
		
		resultTime = new JLabel("Waiting for data...");
		resultTime.setBounds(500, 100, 136, 14);
		contentPane.add(resultTime);
		
		lblNumberOfThread = new JLabel("Number of Thread : ");
		lblNumberOfThread.setBounds(246, 75, 116, 14);
		contentPane.add(lblNumberOfThread);
		lblNumberOfThread.setVisible(false);
		
		threadNB = new JTextField();
		threadNB.setHorizontalAlignment(SwingConstants.CENTER);
		threadNB.setText("4");
		threadNB.setBounds(372, 72, 48, 20);
		contentPane.add(threadNB);
		threadNB.setColumns(10);
		threadNB.setVisible(false);
		
		ActionListener radioListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(arg0.getSource() == radioT)
				{
					threadNB.setVisible(true);
					lblNumberOfThread.setVisible(true);
					
				}
				if(arg0.getSource() == radioN)
				{
					threadNB.setVisible(false);
					lblNumberOfThread.setVisible(false);
				}
				contentPane.revalidate();					
			}
		};
		radioT.addActionListener(radioListener);	
		radioN.addActionListener(radioListener);
		
		ActionListener selectListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				set1.setSelected(false);
				set2.setSelected(false);
				set3.setSelected(false);
				set4.setSelected(false);
				set5.setSelected(false);
				
				if(arg0.getSource() == set1){
					set1.setSelected(true);
					list = main.getList1();
				}
					
				if(arg0.getSource() == set2){
					set2.setSelected(true);
					list = main.getList2();
				}
				
				if(arg0.getSource() == set3){
					set3.setSelected(true);
					list = main.getList3();
				}
				
				if(arg0.getSource() == set4){
					set4.setSelected(true);
					list = main.getList4();
				}
				
				if(arg0.getSource() == set5){
					set5.setSelected(true);
					list = main.getList5();
				}
				
				stockPrice.setText(list.get(0).toString());
				strikePrice.setText(list.get(1).toString());
				discountRate.setText(list.get(2).toString());
				maturityTime.setText(list.get(3).toString());
				carryRate.setText(list.get(4).toString());
				volatility.setText(list.get(5).toString());
				nStep.setText(list.get(6).toString());
				nSim.setText(list.get(7).toString());
				contentPane.revalidate();
				
				
			}
		};
		
		set1.addActionListener(selectListener);
		set2.addActionListener(selectListener);
		set3.addActionListener(selectListener);
		set4.addActionListener(selectListener);
		set5.addActionListener(selectListener);
	}
}
